<?php
try {
    if ($argc != 2) {
        throw new Exception("参数错误");
    }
    switch ($argv[1]) {
        case "start":
            $server = new swoole_http_server("localhost", 81);
            $server->set(
                [
                    "worker_num" => 1,
                    "daemonize" => false
                ]
            );
            $server->on("request", function (\Swoole\Http\Request $request, \Swoole\Http\Response $response) {
                $response->end("11111");
            });
            $server->on("Start", function (Swoole\Server $server) {
                echo "启动成功" . PHP_EOL;
                $pid = $server->master_pid;
                file_put_contents("./wolf.pid", $pid);
            });
            $server->on("WorkerStart", function (\Swoole\Server $server, int $workerId) {

            });
            $server->start();
            break;
        case "stop":
            $getPid = intval(file_get_contents("./wolf.pid"));
            if ($getPid && trim($getPid) != 0) {
                \Swoole\Process::kill($getPid);
            }
            echo "服务关闭成功" . PHP_EOL;
            break;
        default:
            throw new Exception("参数错误");
    }
} catch (Exception $exception) {
    echo $exception->getMessage() . PHP_EOL;
}


